package com.springmvc.controller;

import com.springmvc.model.ExpendTable;
import com.springmvc.service.ExpendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value="/expend")
public class ExpendController {
    @Autowired
    ExpendService expendService;
    @RequestMapping(value="expendList")
    public ModelAndView expendList(){
        ModelAndView mv = new ModelAndView();
        List<ExpendTable> expendTablesList = expendService.queryByPage();
        Double totalMoney = 0.00;
        for (int i = 0; i < expendTablesList.size(); i++) {
            totalMoney += expendTablesList.get(i).getExpend();
        }
        mv.addObject("expendTablesList",expendTablesList);
        mv.addObject("totalMoney",totalMoney);
        mv.setViewName("test/expendList");
        return mv;
    }

    @RequestMapping(value="addExpendForm")
    public ModelAndView addExpendForm(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("test/addExpendForm");
        return mv;
    }

    @RequestMapping(value="addExpend")
    @ResponseBody
    public Object addIncome(String content, Float expend){
        ExpendTable expendTable = new ExpendTable();
        expendTable.setContent(content);
        expendTable.setExpend(expend);
        expendTable.setCreatetime(new Date());
        expendService.insert(expendTable);
        return "ok";
    }
}